//
//  PersonalCenterHeader.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/8.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit

protocol PersonalCenterHeaderDelegate: NSObjectProtocol {
    func setHeadAction(sender: UIButton)
    func personalManagementAction(sender: UIButton)
}

class PersonalCenterHeader: UICollectionReusableView {
    
    var myDelegate: PersonalCenterHeaderDelegate!
    
    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initBaseLayout()
        layoutPageSubviews()
        createButton()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - event responds
    func setHeadAction(sender: UIButton) {
        if self.myDelegate.responds(to: #selector(PersonalCenterHeader.setHeadAction(sender:))) {
            self.myDelegate.setHeadAction(sender: sender)
        }
    }
    
    func personalManagementAction(sender: UIButton) {
        if self.myDelegate.responds(to: #selector(PersonalCenterHeader.personalManagementAction(sender:))) {
            self.myDelegate.personalManagementAction(sender: sender)
        }
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(topBackView)
        self.addSubview(headPortrait)
        self.addSubview(nameLabel)
        self.addSubview(singleLine)
        self.addSubview(middleView)
        self.addSubview(bottomFondView)
        self.addSubview(fondLabel)
    }
    
    func layoutPageSubviews() {
        topBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(177+(170/375*SCREEN_WIDTH))
        }
        headPortrait.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(28)
            make.width.height.equalTo(100)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(headPortrait.snp.bottom).offset(14)
            make.width.equalTo(SCREEN_WIDTH-200)
            make.height.equalTo(14)
        }
        
        singleLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(nameLabel.snp.bottom).offset(21)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(1)
        }
        
        middleView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(topBackView.snp.bottom).offset(0)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(16)
        }
        
        bottomFondView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(middleView.snp.bottom).offset(0)
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(46)
        }
        
        fondLabel.snp.makeConstraints { (make) in
            make.centerX.centerY.equalTo(bottomFondView)
            make.width.equalTo(56)
            make.height.equalTo(14)
        }
    }
    
    func createButton() {
        let imageNames = [["icon_wallet_n", "icon_truck_n", "icon_aftersales_n", "icon_reminders_n"], ["icon_coins_n", "icon_likefollow_n", "icon_location_n", "icon_information_n"]]
        let selectedImageNames = [["icon_wallet_h", "icon_truck_h", "icon_aftersales_h", "icon_reminders_h"], ["icon_coins_h", "icon_likefollow_h", "icon_location_h", "icon_information_h"]]
        for i in 0 ..< 2 {
            for j in 0 ..< 4 {
                let button = UIButton(frame: CGRect(x: 13+j*Int(90/375*SCREEN_WIDTH), y: 177+i*Int(80/375*SCREEN_WIDTH), width: Int(80/375*SCREEN_WIDTH), height: Int(80/375*SCREEN_WIDTH)))
                button.setBackgroundImage(UIImage(named: imageNames[i][j]), for: .normal)
                button.setBackgroundImage(UIImage(named: selectedImageNames[i][j]), for: .highlighted)
                self.addSubview(button)
                button.tag = i*4+j+300
                button.addTarget(self, action: #selector(PersonalCenterHeader.personalManagementAction(sender:)), for: .touchUpInside)
            }
        }
    }
    
    func configUI() {
        nameLabel.text = "伸不开的懒腰"
    }
    
    // MARK: - setters and getters
    var _topBackView: UIView!
    var topBackView: UIView {
        if _topBackView == nil {
            _topBackView = UIView()
            _topBackView.backgroundColor = UIColor.white
        }
        return _topBackView
    }
    
    var _headPortrait: UIButton!
    var headPortrait: UIButton {
        if _headPortrait == nil {
            _headPortrait = UIButton()
            _headPortrait.setBackgroundImage(UIImage(named: "icon_personal_n"), for: .normal)
            _headPortrait.setBackgroundImage(UIImage(named: "icon_personal_h"), for: .highlighted)
            _headPortrait.layer.masksToBounds = true
            _headPortrait.layer.cornerRadius = 50
            _headPortrait.addTarget(self, action: #selector(PersonalCenterHeader.setHeadAction(sender:)), for: .touchUpInside)
        }
        return _headPortrait
    }
    
    var _nameLabel: UILabel!
    var nameLabel: UILabel {
        if _nameLabel == nil {
            _nameLabel = UILabel()
            _nameLabel.font = UIFont(name: "PingFang-SC-Regular", size: 16)
            _nameLabel.textColor = UIColor(hexString: "#666666")
            _nameLabel.textAlignment = .center
        }
        return _nameLabel
    }
    
    var _singleLine: UIView!
    var singleLine: UIView {
        if _singleLine == nil {
            _singleLine = UIView()
            _singleLine.backgroundColor = UIColor(hexString: "#DDDDDD")
        }
        return _singleLine
    }
    
    var _middleView: UIView!
    var middleView: UIView {
        if _middleView == nil {
            _middleView = UIView()
            _middleView.backgroundColor = UIColor(hexString: "#E5E5E5")
        }
        return _middleView
    }
    
    var _bottomFondView: UIView!
    var bottomFondView: UIView {
        if _bottomFondView == nil {
            _bottomFondView = UIView()
            _bottomFondView.backgroundColor = UIColor.white
        }
        return _bottomFondView
    }
    
    var _fondLabel: UILabel!
    var fondLabel: UILabel {
        if _fondLabel == nil {
            _fondLabel = UILabel()
            _fondLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _fondLabel.textColor = UIColor(hexString: "#252525")
            _fondLabel.textAlignment = .center
            _fondLabel.text = "猜你喜欢"
        }
        return _fondLabel
       
    }
}
